package json.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.google.gson.Gson;



/**
 * Json工具类
 * ClassName: JsonUtils.java <br/>
 * Description: TODO <br/>
 * Date: 2015-3-30 <br/>
 * <br/>
 *
 * @author chenchunji
 *         修改记录
 * @version 产品版本信息 yyyy-mm-dd 姓名(邮箱) 修改信息<br/>
 */
public final class JsonUtils {
	
	private static final Logger LOGGER = Logger.getLogger(JsonUtils.class);
	
	protected static Gson gson = new Gson();
	
	/**
	 * <p>Title: JsonUtils </p>
	 * <p>Description: Constructor </p>  
	 */
	private JsonUtils() {
	}

	/**
	 * 转换成String的List集合
	 * @param list list
	 * @return ist<String>
	 */
	@SuppressWarnings("rawtypes")
	public static List<String> toJson(List list){
		List<String> listString = null;
		if(RiCheckUtils.isEmpty(list)){
			return listString;
		}
		try{
			listString = new ArrayList<String>();
			for(Object o :list){
				listString.add(gson.toJson(o));
			}
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return listString;
	}
	
    /**
     * 
     * <p>Title: toJson </p>
     * <p>Description: toJson </p>
     * @param set set
     * @return Set<String>
     */
	@SuppressWarnings({ "rawtypes"})
	public static Set<String> toJson(Set set){
		Set<String> setString = null;
		if(RiCheckUtils.isEmpty(set)){
			return setString;
		}
		try{
			setString = new HashSet<String>();
			for(Object o:set){
				if(o instanceof String) {
					setString.add((String)o);
				}else{
					setString.add(gson.toJson(o));
				}
			}
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return setString;
	}
	/**
	 * <p>Title: toJson </p>
	 * <p>Description: toJson </p>
	 * @param o Description
	 * @return String
	 */
	public static String toJson(Object o){
		if(o == null){
			return null;
		}
		//避免两个出现重复双引号
		if(o instanceof String) {
		    return (String)o;
		}
		String jsonString = null;
		try{
			jsonString =  gson.toJson(o);
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return jsonString;
	}

	/**
	 * <p>Title: toJson </p>
	 * <p>Description: toJson </p>
	 * @param map map
	 * @return Map<String,String>
	 */
	@SuppressWarnings({ "rawtypes"})
    public static Map<String,String> toJson(Map map){
			Map<String, String> map2 = new HashMap<String, String>();
			Iterator iterator = map.entrySet().iterator();
			while(iterator.hasNext()){
				Map.Entry entry = (Entry) iterator.next();
				String key = (String) entry.getKey();
				Object value = entry.getValue();
				map2.put(key, JsonUtils.toJson(value));
			}
			return map2;
	}

	/**
	 * <p>Title: fromJson </p>
	 * <p>Description: fromJson </p>
	 * @param jsonString jsonString
	 * @param clazz clazz
	 * @return Object
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object fromJson(String jsonString,Class clazz){
		Object o = null;
		if(jsonString == null){
			return o;
		}
		
		try{
			o = gson.fromJson(jsonString, clazz);
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return o;
	}
    /**
     * <p>Title: fromJson </p>
     * <p>Description: fromJson </p>
     * @param listString listString
     * @param clazz clazz
     * @return List
     */
	@SuppressWarnings({ "rawtypes", "unchecked"})
	public static List fromJson(List<String> listString,Class clazz){
		List list = null;
		if(RiCheckUtils.isEmpty(listString)){
			return list;
		}
		list = new ArrayList();
		try{
			for(String jsonString :listString){
				list.add(gson.fromJson(jsonString, clazz));
			}
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return list;
	}
    /**
     * <p>Title: fromJson </p>
     * <p>Description: fromJson </p>
     * @param setString setString
     * @param clazz clazz
     * @return Set
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Set fromJson(Set<String> setString,Class clazz){
		Set set = null;
		if(RiCheckUtils.isEmpty(setString)){
			return set;
		}
		set = new HashSet();
		try{
			for(String jsonString :setString){
				set.add(gson.fromJson(jsonString, clazz));
			}
		}catch(Exception e){
			LOGGER.error(e.getMessage(), e);
		}
		return set;
	}
}
